<template>
	<div class="container">
		<!-- 主导航栏 -->
		<ul class="main-tabs clearfix">
			<li v-for="(item,index) in mainTabs.data" :key="index" :class="{active:index==mainTabs.index}" @click="mainTabSwitch(index)">{{item.title}}</li>
		</ul>
		
		<!-- 头部信息 -->
		<h3 class="header-title clearfix">
			<span></span>
			<p>{{headerTitle}}</p>
		</h3>
		
		<!-- 副导航栏 -->
		<comNavTabs :navList="navList" @switch="tabSwitch"></comNavTabs>
		<!-- 数据展示的列表 -->
		<comTable :options="tableList"	@openConDialog="openContractDialog"
		></comTable>
		<!-- 分页器 -->
		<div class="pageBox">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="pageChange"
				:current-page.sync="cur"
				:page-sizes="[10, 20, 30, 50, 100]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="totalNum"
				background
			></el-pagination>
		</div>
		<!-- 发送/审核/查看模板 -->
		<contractDialog :options="contract" @clickReject="clickReject"
			@subContract="subContract"
		></contractDialog>
	</div>
</template>

<script>
export default{
	components:{
		comNavTabs: () => import("@/household/common/comNavTabs.vue"),
		comTable: () => import("@/household/common/comTable.vue"),
		contractDialog: () => import("@/component/contract/contractDialog.vue")
	},
	data(){
		return {
			/* 主导航栏列表数据 */
			mainTabs: {
				index:0,		//当前是在哪个tab栏
				data:[
					{title:'电子合同认证'},
					{title:'敲章人认证'},
					// {title:'审核人认证'}
				]	
			},
			headerTitle: '电子合同认证',								//头部标题
			/* 副导航栏列表数据 */
			navList:{
				index:0,		//当前是在哪个tab栏
				data:[
					{title:'待发送'},
					{title:'待审核'},
					{title:'已通过'},
					{title:'未通过'}
				]	
			},
			/* 表格数据 */
			tableList:{
				list:[
					{title:'企业名称',prop:'companyname',isClick:false},
					{title:'所在地区',prop:'city',isClick:false},
					{title:'企业类型',prop:'companytype',isClick:false},
					{title:'企业规模',prop:'companysize',isClick:false},
					{title:'申请时间',prop:'createTime',isClick:false}
				],
				noOperation:true,	//是否有操作项
				operation:[			//操作项内容
					{name:'发送',isBtn:false,title:'openConDialog'}
				],
				data:[]
			},
			/* 电子合同数据 */
			contract:{
				lookPDFDialog: false,								//电子合同发送/查看模板
				btnTextl: '资料驳回',								//按钮左内容
				btnTextr: '确认发送完成',								//按钮右内容
				companyTitle: '上海天都人力资源有限公司',				//企业名称
				data:{
					agentName: '',										//敲章人姓名
					agentTelnum: '',									//敲章人手机号
					auditName: '',										//审核人姓名
					auditTelnum: '',									//审核人手机号
					applicationFormUrl: '',								//证书申请表pdf
					authorizationUrl: '',								//委托书pdf
					auditorAttorneyUrl: '',								//审核人pdf
					businessLicenseUrl:'',								//企业营业执照照片
					agentIdCardUrl: '',									//法人身份证复印件照片
					corporateIdentityCardUrl: '',						//敲章人身份证照片
					auditorCardCopiesUrl: '',							//审核人身份证照片
				},
				textarea: '',										//驳回理由
				isContract: true,									//当前是否显示三张照片
				agentShow: false,									//是否显示敲章人信息
				auditShow: false,									//是否显示审核人信息
				showBtn: false,										//是否展示按钮栏和驳回理由
				showTextarea: false,								//是否展示驳回理由框
				placeholder: '',									//占位符含义
				tableData:	[
					/* {
						applyTime: '2016-05-02',					//申请时间
						createTime: '2016-05-02',					//审核时间
						operator: '王小虎',							//操作人
						auditResult: '未通过',						//审核结果
						cause: '资料不全',							//备注	
					} */
				]
			},
			
			
			/* 分页功能数据 */
			cur:1,
			pageSize: 10,
			totalNum: 0,				//总共有多少条数据,根据这个数据去适配页数
		}
	},
	created(){
		this.getDataList();
	},
	methods:{
		// 主tab栏切换
		mainTabSwitch(index){
			console.log(index);
			this.mainTabs.index = index;
			index==0?this.headerTitle="电子合同认证":'';
			index==1?this.headerTitle="敲章人认证":'';
			// index==2?this.headerTitle="审核人认证":'';
			this.cur = 1;
			this.pageSize = 10;
			this.tabSwitch(0);
		},
		// tab栏切换
		tabSwitch(index){
			console.log(index);
			this.navList.index = index;
			this.tableList.list.splice(0);
			this.navList.data.splice(0);
			this.tableList.operation.splice(0);
			this.tableList.list = [
				{title:'企业名称',prop:'companyname',isClick:false},
				{title:'所在地区',prop:'city',isClick:false},
				{title:'企业类型',prop:'companytype',isClick:false},
				{title:'企业规模',prop:'companysize',isClick:false},
				{title:'申请时间',prop:'createTime',isClick:false}
			];
			if(this.mainTabs.index == 0 || this.mainTabs.index == 1){
				this.navList.data = [
					{title:'待发送'},
					{title:'待审核'},
					{title:'已通过'},
					{title:'未通过'}
				]
				if(this.navList.index == 0){
					this.tableList.operation = [
						{name:'发送',isBtn:false,title:'openConDialog'}
					];
				}else if(this.navList.index == 1){
					this.tableList.operation = [
						{name:'审核',isBtn:false,title:'openConDialog'}
					];
				}else if(this.navList.index == 2 || this.navList.index == 3){
					this.tableList.operation = [
						{name:'查看',isBtn:false,title:'openConDialog'}
					];
				}
			}else{
				this.navList.data = [
					{title:'待审核'},
					{title:'已通过'},
					{title:'未通过'}
				]
				if(this.navList.index == 0){
					this.tableList.operation = [
						{name:'审核',isBtn:false,title:'openConDialog'}
					];
				}else if(this.navList.index == 1 || this.navList.index == 2){
					this.tableList.operation = [
						{name:'查看',isBtn:false,title:'openConDialog'}
					];
				}
			}
			this.getDataList();
		},
		// 获取数据列表
		getDataList(){
			let params = {
				pagenum: this.cur,
				pagesize: this.pageSize
			};
			this.mainTabs.index == 0?params.status = this.navList.index + 1: '';
			this.mainTabs.index == 1?params.status = this.navList.index + 5: '';
			// this.mainTabs.index == 2?params.status = this.navList.index + 9: '';
			this.$eleconAxios.post('/electronic/dajiqqian/selectAuthList',params).then(res => {
				console.log(res);
				if(res.data.code === 1000){
					this.tableList.data.splice(0);
					let result = res.data.data;
					result.listResponses.forEach(item => {
						this.tableList.data.push(item);
					});
					this.totalNum = result.totalNum;
				}else{
					this.$message({
						message: res.data.message,
						type: 'warning'
					})
				}
			})
		},
		// 打开电子合同模板
		openContractDialog(data){
			console.log(data);
			this.contract.lookPDFDialog = true;
			//https://house.fcw-china.com
			this.$eleconAxios.post('/electronic/dajiqqian/selectAuthenMessagePC',{companyId: data.companyid}).then(res => {
				console.log(res);
				if(res.data.code === 1000){
					if(this.mainTabs.index == 0 && res.data.data.appContractCompanyAuthentication){
						this.contract.data = res.data.data.appContractCompanyAuthentication;
						this.contract.companyTitle = data.companyname;
						this.contract.tableData.splice(0);
						this.contract.isContract = true;
						this.contract.agentShow = true;
						this.contract.auditShow = true;
						if(this.navList.index == 0){
							this.contract.btnTextl = "资料驳回";
							this.contract.btnTextr = "确认发送完成";
							this.contract.showBtn = true;
							this.contract.showTextarea = false;
						}else if(this.navList.index == 1){
							this.contract.btnTextl = "审核未通过";
							this.contract.btnTextr = "审核通过";
							this.contract.showBtn = true;
							this.contract.showTextarea = false;
						}else if(this.navList.index == 2 || this.navList.index == 3){
							this.contract.showBtn = false;
						}
						res.data.data.companyRecord.forEach(item => {
							if(item.auditResult == '1'){
								item.auditResult = '待审核';
							}else if(item.auditResult == '2'){
								item.auditResult = '审核通过';
							}else{
								item.auditResult = '审核不通过';
							}
							this.contract.tableData.push(item);
						});
					}else if(this.mainTabs.index == 1 && res.data.data.appContractCompanyAgent){
						this.contract.data = res.data.data.appContractCompanyAgent;
						this.contract.companyTitle = data.companyname;
						this.contract.tableData.splice(0);
						this.contract.isContract = false;
						this.contract.agentShow = true;
						this.contract.auditShow = false;
						if(this.navList.index == '0'){
							this.contract.btnTextl = "资料驳回";
							this.contract.btnTextr = "确认发送完成";
							this.contract.showBtn = true;
							this.contract.showTextarea = false;
						}else if(this.navList.index == '1'){
							this.contract.btnTextl = "审核未通过";
							this.contract.btnTextr = "审核通过";
							this.contract.showBtn = true;
							this.contract.showTextarea = false;
						}else if(this.navList.index == '2' || this.navList.index == '3'){
							this.contract.showBtn = false;
						}
						res.data.data.agentRecord.forEach(item => {
							if(item.auditResult == '1'){
								item.auditResult = '待审核';
							}else if(item.auditResult == '2'){
								item.auditResult = '审核通过';
							}else{
								item.auditResult = '审核不通过';
							}
							this.contract.tableData.push(item);
						});
					}else if(this.mainTabs.index == 2 && res.data.data.appContractCompanyAuditor){
						this.contract.data = res.data.data.appContractCompanyAuditor;
						this.contract.companyTitle = data.companyname;
						this.contract.tableData.splice(0);
						this.contract.isContract = false;
						this.contract.agentShow = false;
						this.contract.auditShow = true;
						if(this.navList.index == '0'){
							this.contract.btnTextl = "审核未通过";
							this.contract.btnTextr = "审核通过";
							this.contract.showBtn = true;
							this.contract.showTextarea = false;
						}else if(this.navList.index == '1' || this.navList.index == '2'){
							this.contract.showBtn = false;
						}
						res.data.data.auditRecords.forEach(item => {
							if(item.auditResult == '1'){
								item.auditResult = '待审核';
							}else if(item.auditResult == '2'){
								item.auditResult = '审核通过';
							}else{
								item.auditResult = '审核不通过';
							}
							this.contract.tableData.push(item);
						});
					}
					
				}else{
					this.$message({
						message: res.data.message,
						type: 'warning'
					})
				}
			})
		},
		// 点击驳回或者取消按钮
		clickReject(){
			if(!this.contract.showTextarea){
				this.contract.showTextarea = true;
				this.contract.btnTextl = '取消';
				this.contract.btnTextr = '确定';
				if(this.current == '0'){
					this.contract.placeholder = '驳回理由';
				}else{
					this.contract.placeholder = '未通过理由';
				}
			}else{
				this.contract.showTextarea = false;
				if(this.current == '0'){
					this.contract.btnTextl = '资料驳回';
					this.contract.btnTextr = '确认发送完成';
				}else{
					this.contract.btnTextl = '审核未通过';
					this.contract.btnTextr = '审核通过';
				}
			}
		},
		// 提交电子合同认证资料
		subContract(){
			let params = {};
			if(this.mainTabs.index == 0){
				params.type = '1';
			}else if(this.mainTabs.index == 1){
				params.type = '2';
			}else{
				params.type = '3';
			}
			if(this.contract.showTextarea){
				if(!this.contract.textarea){
					this.$message({
						message: '理由不能为空！',
						type: 'warning'
					})
					return false;
				}else{
					params.status = '3';
					params.cause = this.contract.textarea;
				}
			}else{
				if(this.navList.index == '0' && this.mainTabs.index != 2){
					params.send = '1';
				}else if((this.navList.index == '1' && this.mainTabs.index != 2) || (this.navList.index == '0' && this.mainTabs.index == 2)){
					params.status = '2';
				}
			}
			params.operator = JSON.parse(localStorage.getItem('userinfo')).username;
			params.companyId = this.contract.data.companyId;
			console.log(params);
			//https://house.fcw-china.com
			this.$eleconAxios.post('/electronic/dajiqqian/updateCompanyAuth',params).then(res => {
				console.log(res);
				if(res.data.code === 1000){
					this.$message({
						message: '操作成功！',
						type: 'success'
					})
					this.contract.textarea = '';
					this.getDataList();
				}else{
					this.$message({
						message: res.data.message,
						type: 'success'
					})
				}
			})
			this.contract.lookPDFDialog = false;
			
		},
		
		//当前页是第几页,点击的第几页的时候调用这个函数
		pageChange(p) {
			// 点击的时候传过来一个参数，这个参数就是所点击的当前页数，
			// 然后把这个当前页数转换成字符串形式赋值给page，
			// 然后把page这个参数传给search这个方法
			console.log(p);
			this.cur = p;
			this.getDataList();
		},
		//每页有多少条,10,20,50等,点击选择的时候调用这个方法
		handleSizeChange (val) {
			console.log(val);
			this.pageSize = val;
			this.getDataList();
		},
	}
}
</script>

<style scoped="scoped">
.container{background-color: #fff;height: 100%;padding-left: 20px;}
.main-tabs {background-color: #EFF1F8;margin-bottom: 20px;margin-left: -20px;padding-left: 50px;}
.main-tabs li{height: 40px;line-height: 40px;float: left;margin: 10px 20px;padding: 0 30px;}
.main-tabs .active{background: #0064E4;border-radius: 20px;color: #fff;}
.header-title{padding: 0 0 20px 0;border-bottom: 1px solid #D2D2D2;}
.header-title span{width: 15px;height: 30px;background-color: #0064E4;margin-right: 20px;float: left;}
.header-title p{float: left;font-weight: bold;line-height: 30px;}
.pageBox{margin-top: 20px;}
</style>
